package com.zjdiepu.www.biz.fk;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.AuditListStatus;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.ModeIDType;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessDetailMapper;
import com.zjdiepu.www.dao.fk.FkAuditListMapper;
import com.zjdiepu.www.dao.yw.YwBusinessCarMapper;
import com.zjdiepu.www.dao.yw.YwBusinessLoanMapper;
import com.zjdiepu.www.dao.yw.YwBusinessMapper;
import com.zjdiepu.www.ibiz.fk.IFkAuditListService;
import com.zjdiepu.www.ibiz.zpp.FengKongShenHeService;
import com.zjdiepu.www.model.fk.dto.FkAuditList;
import com.zjdiepu.www.model.fk.ext.FkAuditListExt;
import com.zjdiepu.www.model.yw.response.YwBusinessDetail;

/**
 * Service层操作实现<br/>
 * @author zhangpp<br/>
 * @time 2017年06月21日 20时22分<br/>
 * @since 1.0<br/>
 */
@Service
public class FkAuditListServiceImpl extends BaseService implements IFkAuditListService {
	public static final Logger logger = LoggerFactory.getLogger(FkAuditListServiceImpl.class);

	@Autowired
	private YwBusinessMapper ywBusinessMapper;
	@Autowired
	private YwBusinessCarMapper ywBusinessCarMapper;
	@Autowired
	private YwBusinessLoanMapper ywBusinessLoanMapper;
	@Autowired
	private FkAuditListMapper fkAuditListMapper;
	@Autowired
	private YwBusinessDetailMapper ywBusinessDetailMapper;
	@Autowired
	private FengKongShenHeService fKongShenHeService;
	
	/**
	 * 获取待审批记录
	 * @author xiongxiaotun <br/>
	 * @Title: getFkAuditListFromBusi  <br/>
	 * @param ywBusinessDetail
	 * @param modeIDType
	 * @param source
	 * @param currentUser
	 * @return
	 * @version V1.0  <br/>
	 */
	private FkAuditList getFkAuditListFromBusi(YwBusinessDetail ywBusinessDetail,
			ModeIDType modeIDType, String source, AbstractSysUser currentUser){
		if(ywBusinessDetail == null || modeIDType == null) {
			return null;
		}
		FkAuditList fkAuditList_ = new FkAuditList();
		fkAuditList_.setDpStatus(DpStatus.NORMAL.getCode());
		fkAuditList_.setBusiId(ywBusinessDetail.getId());
		fkAuditList_.setModeId(modeIDType.getCode());
		fkAuditList_.setAuditListStatus(Integer.valueOf(AuditListStatus.WEI_SHEN_PI.getCode()));
		fkAuditList_.setStatusList(Integer.valueOf(AuditListStatus.WEI_SHEN_PI.getCode()));
		FkAuditList fkAuditList__ = fkAuditListMapper.selectRefined(fkAuditList_);
		
		FkAuditList fkAuditList = null;
		if(null == fkAuditList__) {
			ywBusinessDetailMapper.mapper(ywBusinessDetail);
			fkAuditList = new FkAuditList();
			fkAuditList.initInsertData(currentUser.getId(), currentUser.getName());
			fkAuditList.setModeId(modeIDType.getCode());
			fkAuditList.setBusiId(ywBusinessDetail.getId());
			fkAuditList.setBusiConfirmTime(ywBusinessDetail.getSubmitTime());
			fkAuditList.setLoanAmount(ywBusinessDetail.getRealLoanAmount());
			fkAuditList.setChannelType(ywBusinessDetail.getChannelType());
			fkAuditList.setChannelTypeName(ywBusinessDetail.getChannelTypeDisplay());
			fkAuditList.setChannelStatus(ywBusinessDetail.getChannelStatus());
			fkAuditList.setChannelStatusName(ywBusinessDetail.getChannelStatusDisplay());
			fkAuditList.setSource(source);
			fkAuditList.setDealer(ywBusinessDetail.getDealerName());
			fkAuditList.setDepartmentName(ywBusinessDetail.getOrgName());
			fkAuditList.setSalersmanName(ywBusinessDetail.getSalesmanName());
			fkAuditList.setCusName(ywBusinessDetail.getCustomerName());
			fkAuditList.setCusIdno(ywBusinessDetail.getCustomerNo());
			fkAuditList.setBankname(ywBusinessDetail.getLoanBankDisplay());
			fkAuditList.setAuditListStatus(Integer.valueOf(AuditListStatus.WEI_SHEN_PI.getCode()));
			fkAuditList.setOperateStatus(Integer.valueOf(AuditListStatus.WEI_SHEN_PI.getCode()));
			fkAuditList.setStatusList(Integer.valueOf(AuditListStatus.WEI_SHEN_PI.getCode()));
		}
		return fkAuditList;
	}
	
	@Override
	public void insertFkAuditList(YwBusinessDetail ywBusinessDetail,
			ModeIDType modeIDType, String source, AbstractSysUser currentUser) {
		FkAuditList fkAuditList = getFkAuditListFromBusi(ywBusinessDetail, modeIDType, source, currentUser);
		if(null == fkAuditList) {
			return;
		}
		fkAuditListMapper.insert(fkAuditList);
	}
	
	@Override
	public void insertFkAuditList(YwBusinessDetail ywBusinessDetail,
			ModeIDType modeIDType, String source, AbstractSysUser currentUser,
			String ziShenYiShenName) {
		FkAuditList fkAuditList = getFkAuditListFromBusi(ywBusinessDetail, modeIDType, source, currentUser);
		if(null == fkAuditList) {
			return;
		}
		//资审一审人员名称
		fkAuditList.setCcol1(ziShenYiShenName);
		fkAuditListMapper.insert(fkAuditList);
	}
	@Override
	public void saveUpdateAndDelete(FkAuditList fkAuditList, OperateType operType) {
		switch (operType) {
		case add:
			fkAuditList.initInsertData(fkAuditList.getModifier(), fkAuditList.getModifierName());
			fkAuditListMapper.insert(fkAuditList);
			break;
		case edit:
			fkAuditList.initUpdateData(fkAuditList.getModifier(), fkAuditList.getModifierName());
			// 影响的记录数
			int updateCount = fkAuditListMapper.updateByPrimaryKeySelective(fkAuditList);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			fkAuditList.initDeleteData(fkAuditList.getModifier(), fkAuditList.getModifierName());
			// 影响的记录数
			int deleteCount = fkAuditListMapper.updateByPrimaryKeySelective(fkAuditList);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<FkAuditList> selectByExt(FkAuditListExt fkAuditListExt) {
		return fkAuditListMapper.selectByExt(fkAuditListExt);
	}
	
	@Override
	public int countByExt(FkAuditListExt fkAuditListExt){
		return fkAuditListMapper.countByExt(fkAuditListExt);
	}
	
	@Override
	public FkAuditList selectById(String id) {
		return fkAuditListMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			fkAuditListMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(FkAuditList fkAuditList) {
		return fkAuditListMapper.countRefined(fkAuditList);
	}

	@Override
	public FkAuditList selectRefined(FkAuditList fkAuditList) {
		return fkAuditListMapper.selectRefined(fkAuditList);
	}
	
	@Override
	public int deletedRefined(FkAuditList fkAuditList) {
		return fkAuditListMapper.deletedRefined(fkAuditList);
	}

	@Override
	public boolean repeat(FkAuditList fkAuditList) {
		int count = countRefined(fkAuditList);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(fkAuditList.getId())) {
			return true;
		}
		FkAuditList dbItem = selectRefined(fkAuditList);
		if(null == dbItem) {
			return false;
		}
		return !fkAuditList.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return fkAuditListMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<FkAuditList> fkAuditLists) {
		return fkAuditListMapper.insertBatch(fkAuditLists);
	}
}
